//
//  RadioViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/30.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioBannerTableViewCell.h"
#import "RadioTableViewCell.h"
#import "RadioRootModel.h"
#import "RadioBanners.h"
#import "Program_List_All.h"
#import "GXNetTool.h"
#import "CFMacro.h"

#define kTableViewCellReuse @"radioTableViewCellReuse"
#define kBannerCellReuse @"radioBanner"

@interface RadioViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) RadioRootModel *root;
@end

@implementation RadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"RadioTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellReuse];
    [_tableView registerNib:[UINib nibWithNibName:@"RadioBannerTableViewCell" bundle:nil] forCellReuseIdentifier:kBannerCellReuse];
}
#pragma mark - data
- (void)handleData {
    // 电台页内容
    [GXNetTool GET:kRadioURL(0, 0, 0) andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
        RadioRootModel *radioModel = [RadioRootModel modelWithDictionary:result];
        _root = radioModel;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}
#pragma mark - tbleView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_root) {
        return _root.program_list_all.count;
    } else {
        return 10;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_root.program_list_all[indexPath.row].banners) {
        RadioBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kBannerCellReuse];
        cell.banners = _root.program_list_all[indexPath.row].banners;
        return cell;
    } else {
        RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
        if (_root) {
            cell.programlist = _root.program_list_all[indexPath.row];
        }
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.program_list_all[indexPath.row].banners) {
        return 120;
    } else if (indexPath.row == 0) {
        return 120;
    } else {
        return 80;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
